<?php

/**
 * Created on 7 mai 2011
 *  * @author Samy Deghou(deghou@polytech.unice.fr ; deghou@embl.de)
 * 		   Stephan Beisken (beisken@ebi.ac.uk)
 * 		   Luis Felipe De Figueiredo (ldpf@ebi.ac.uk)
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
class Spectrum implements iCRUD {
    /*
     * Attributes
     */

    private $_spectrumID;
    private $_date;
    private $_fingerprint;
    private $_specFile;
    private $_comments;
    private $_reference;
    private $_assignedParent;

    /*
     * Constructor
     */

    public function Spectrum($date, $fingerprint, $specfile, $comments, $reference, $assignedParent, $spectrumID=-1) {
        $this->_date = $date;
        $this->_fingerprint = $fingerprint;
        $this->_specFile = $specfile;
        $this->_comments = $comments;
        $this->_reference = $reference;
        $this->_assignedParent = $assignedParent;
        $this->_spectrumID = $spectrumID;
    }

    /*
     * Accessors
     */

    public function getSpectrumID() {
        return $this->_SpectrumID;
    }

    public function getTheDate() {
        return $this->_date;
    }

    public function getFingerPrint() {
        return $this->_fingerprint;
    }

    public function getSpecFile() {
        return $this->_specFile;
    }

    public function getComments() {
        return $this->_comments;
    }

    public function getassignedParent() {
        return $this->_assignedParent;
    }

    public function getReference() {
        return $this->_reference;
    }

    /*
     * Modifiers
     */

    public function setSpectrumID($param) {
        return $this->_SpectrumID = $param;
    }

    public function setTheDate($param) {
        return $this->_date = $param;
    }

    public function setFingerPrint($param) {
        return $this->_fingerprint = $param;
    }

    public function setSpecFile($param) {
        return $this->_specFile = $param;
    }

    public function setComments($param) {
        return $this->_comments = $param;
    }

    public function setassignedParent($param) {
        return $this->_assignedParent = $param;
    }

    public function setReference($param) {
        return $this->_reference = $param;
    }

    /*
     * CRUD
     */

    /**
     * Put an object in the database
     */
    public function create() {
        Database::connect();
        if ($this->_spectrumID != -1)
            die("Unable to create, still existing !");
        else {
            $query = "INSERT INTO `spectrum` values (NULL,'" .
                    $this->_date . "','" .
                    $this->_fingerprint . "','" .
                    $this->_specFile . "','" .
                    $this->_comments . "','" .
                    $this->_reference . "','" .
                    $this->_assignedParent . "')";
            mysql_query($query) or die(mysql_error());
            $this->_idSujet = mysql_insert_id();
            Database::disconnect();
        }
        return $this->_idSujet;

        Database::disconnect();
    }

    /**
     * Read an object from the database
     */
    public static function read($key) {
        Database::connect();
        $query = "SELECT * FROM SPECTRUM WHERE spectrum_id = '" . $key . "'";
        $ans = mysql_fetch_assoc(mysql_query($query));
        extract($ans);
        Database::disconnect();
        return new Spectrum($date, $fingerprint, $spec_file, $comments, $reference, $assigned_parent, $spectrum_id);
        Database::disconnect();
    }

    /**
     * Update an object from the database
     */
    public function update() {
        Database::connect();
        $query = "UPDATE `SPECTRUM` SET date='" .
                $this->_date . "', fingerprint='" .
                $this->_fingerprint . "', spec_file='" .
                $this->_specFile . "', comments='" .
                $this->_comments . "', reference='" .
                $this->_reference . "', assigned_parent='" .
                $this->_assignedParent . "', assigned_molecule='" .
                $this->_spectrumID . "'";
        mysql_query($query) or die(mysql_error());
        Database::disconnect();
    }

    /**
     * Delete an object from the database
     */
    public function delete() {
        Database::connect();
        $query = "DELETE FROM `SPECTRUM` WHERE spectrum_id='" .
                $this->_spectrumID . "'";
        mysql_query($query) or die(mysql_error());
        Database::disconnect();
    }

}
?>
